﻿//***********************************************************************************
// 文件名称：Global
// 功能描述：全局数据类
// 数据表：
// 作者：liuxiaohu
// 日期：2017/3/13 9:12:20
// 修改记录：
//***********************************************************************************

using System;
using System.Threading;
using System.Configuration;

namespace GameServer
{
    /// <summary>
    /// 全局类
    /// </summary>
    public class Global
    {
        #region 变量

        /// <summary>
        /// 排行榜管理对象
        /// </summary>
        public static RankManager RankMgr { get; private set; }

        /// <summary>
        /// 玩家管理对象
        /// </summary>
        public static PlayerManager PlayerMgr { get; private set; }

        /// <summary>
        /// MySql连接字符串
        /// </summary>
        public static String MySqlConString { get; private set; }

        /// <summary>
        /// 提供给外部获取的全局时间变量
        /// </summary>
        public static DateTime GlobalTime { get { return globalTime; } }

        /// <summary>
        /// 全局时间对象
        /// </summary>
        private static DateTime globalTime;

        /// <summary>
        /// 线程对象
        /// </summary>
        private static Thread runThread;

        /// <summary>
        /// 服务器运行标志位
        /// </summary>
        private static volatile bool run;

        /// <summary>
        /// 测试活动对象
        /// </summary>
        private static TestActivity testActivity;

        #endregion

        #region 方法

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns>成功返回true</returns>
        public static bool Init()
        {
            globalTime = System.DateTime.Now;

            // 初始化连接字符串
            MySqlConString = ConfigurationManager.AppSettings["MySqlConnectionStr"];

            // 初始化日志记录
            LOG.Init();

            // 初始化排行榜
            RankMgr = new RankManager();
            if (!RankMgr.Init())
            {
                return false;
            }

            // 初始化玩家列表
            PlayerMgr = new PlayerManager();
            if (!PlayerMgr.Init())
            {
                return false;
            }

            // 初始化测试活动
            InitActivity();

            // 启动线程
            run = true;
            runThread = new Thread(Run);
            runThread.Start();

            LOG.Write("GameServer-Start");

            return true;
        }

        private static void InitActivity()
        {
            // 从配置文件中读取活动开启周期
            String[] openTimePeriod = ConfigurationManager.AppSettings["openTimePeriod"].Split(';');
            Int32[] tmpOpenTimePeriod = new Int32[14];
            Int32 i = 0;
            foreach (String time in openTimePeriod)
            {
                // 选择字符串中的整数
                string result = System.Text.RegularExpressions.Regex.Replace(time, @"[^-?\d+$]+", "");
                tmpOpenTimePeriod[i++] = Convert.ToInt32(result);
            }

            // 从配置文件中读取活动生命周期
            String[] lifeTimePeriod = ConfigurationManager.AppSettings["lifeTimePeriod"].Split(';');
            Int32[] tmpLifeTimePeriod = new Int32[14];
            i = 0;
            foreach (String time in lifeTimePeriod)
            {
                // 选择字符串中的整数
                string result = System.Text.RegularExpressions.Regex.Replace(time, @"[^-?\d+$]+", "");
                tmpLifeTimePeriod[i++] = Convert.ToInt32(result);
            }

            // 创建活动
            testActivity = new TestActivity(
            new TimePeriod(tmpOpenTimePeriod[0], tmpOpenTimePeriod[1], tmpOpenTimePeriod[2], tmpOpenTimePeriod[3], tmpOpenTimePeriod[4], tmpOpenTimePeriod[5], tmpOpenTimePeriod[6],
            tmpOpenTimePeriod[7], tmpOpenTimePeriod[8], tmpOpenTimePeriod[9], tmpOpenTimePeriod[10], tmpOpenTimePeriod[11], tmpOpenTimePeriod[12], tmpOpenTimePeriod[13]),
            new TimePeriod(tmpLifeTimePeriod[0], tmpLifeTimePeriod[1], tmpLifeTimePeriod[2], tmpLifeTimePeriod[3], tmpLifeTimePeriod[4], tmpLifeTimePeriod[5], tmpLifeTimePeriod[6],
            tmpLifeTimePeriod[7], tmpLifeTimePeriod[8], tmpLifeTimePeriod[9], tmpLifeTimePeriod[10], tmpLifeTimePeriod[11], tmpLifeTimePeriod[12], tmpLifeTimePeriod[13])
            );
        }

        /// <summary>
        /// 服务器关闭时调用
        /// </summary>
        public static void End()
        {
            run = false;

            LOG.Write("GameServer-Stop");
            // TODO:服务器关闭时需要进行的操作
        }

        /// <summary>
        /// 线程入口函数
        /// </summary>
        private static void Run()
        {
            while (run)
            {
                if (globalTime.Second != DateTime.Now.Second)
                {
                    globalTime = DateTime.Now;

                    GameSecLoop();
                }
            }
        }

        /// <summary>
        /// 秒循环函数
        /// </summary>
        private static void GameSecLoop()
        {
            // 活动秒循环
            testActivity.GameSecLoop();
        }

        #endregion
    }
}